﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using DigitalPlatform.Components;

namespace DigitalPlatform.Common.Convert
{
    public class DeviceItemConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ass = Assembly.Load("DigitalPlatform.Components");
            var type = ass.GetType("DigitalPlatform.Components." + value.ToString());
            if (type == null) return null;
            var ctr = Activator.CreateInstance(type) as ComponentBase;

            Binding binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("DeleteCommand");
            //绑定删除命令
            ctr.SetBinding(ComponentBase.DeleteCommandProperty, binding);
            //绑定删除命令相关参数
            ctr.SetBinding(ComponentBase.DeleteCommandParameterProperty, new Binding() { Source = ctr, Path = new System.Windows.PropertyPath(".") });
            //绑定选中项
            ctr.SetBinding(ComponentBase.IsSelectedProperty, new Binding("IsSelected"));
            //绑定项绑定到Height   流程：组件实际高度<->DataContext.Height<->组件中依赖属性ShowHeight,此处需要设置为双向绑定
            ctr.SetBinding(ComponentBase.ShowHeightProperty, new Binding("Height") { Mode = BindingMode.TwoWay });
            //绑定项绑定到Width
            ctr.SetBinding(ComponentBase.ShowWidthProperty, new Binding("Width") { Mode = BindingMode.TwoWay });

            return ctr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
